﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using QuanLyCongViec.Database;

namespace QuanLyCongViec.Database
{
    public class dbLoaiHopDong
    {
        SystemGlobal gb = new SystemGlobal();
        private SqlConnection conn;
        private SqlDataAdapter dadapt;
        private SqlCommand command;
        private string selectCommand;
        public dbLoaiHopDong()
        {
            string ConnectionString = @"Data Source=SERVER2K3\SQLSV2005;Initial Catalog=QLCongViec;User ID=sa;Password=123";
            conn = new SqlConnection(ConnectionString);
        }
        public DataTable LoaiHopDongGet(string strLoaiHopDongID, string strMaLoaiHopDong)
        {
            if (strLoaiHopDongID != string.Empty)
            {
                selectCommand = @"select * from LoaiHopDong where (1 = 1) 
                                    clauseLoaiHopDongID
                                    clauseMaLoaiHopDong";
            }
            else
            {
                selectCommand = @"select * from LoaiHopDong where (1 = 1) 
                                    clauseMaLoaiHopDong";
            }
            string clauseLoaiHopDongID = "and LoaiHopDongID ='" + strLoaiHopDongID + "'";
            string clauseMaLoaiHopDong = "and MaLoaiHopDong like" + "'%" + strMaLoaiHopDong + "%'";
            selectCommand = gb.Replace(selectCommand, "clauseLoaiHopDongID", clauseLoaiHopDongID,
                                                  "clauseMaLoaiHopDong", clauseMaLoaiHopDong);

            dadapt = new SqlDataAdapter(selectCommand, conn);
            DataSet dsFinal = new DataSet();
            dadapt.Fill(dsFinal);
            return dsFinal.Tables[0];
        }
        public void LoaiHopDongCreate(string strMaLoaiHopDong,string strTenLoaiHopDong,string strThoiGianNhac)
        {
            conn.Open();
            string InsertCommand = @"insert into LoaiHopDong(MaLoaiHopDong,TenLoaiHopDong,ThoiGianNhac)
                                     values('strMaLoaiHopDong','strTenLoaiHopDong',strThoiGianNhac)";
            InsertCommand = gb.Replace(InsertCommand,"strMaLoaiHopDong",strMaLoaiHopDong,
                                                        "strTenLoaiHopDong",strTenLoaiHopDong,
                                                        "strThoiGianNhac",strThoiGianNhac);
            command = new SqlCommand(InsertCommand, conn);
            command.ExecuteNonQuery();
            conn.Close();
        }
        public void LoaiHopDongUpdate(string strLoaiHopDongID,string strMaLoaiHopDong, string strTenLoaiHopDong, string strThoiGianNhac)
        {
            conn.Open();
            string UpdateCommand = @"update LoaiHopDong set MaLoaiHopDong = 'strMaLoaiHopDong',
                                                            TenLoaiHopDong = 'strTenLoaiHopDong',
                                                            ThoiGianNhac = strThoiGianNhac
                                        where LoaiHopDongID = strLoaiHopDongID";
            UpdateCommand = gb.Replace(UpdateCommand, "strMaLoaiHopDong", strMaLoaiHopDong,
                                                        "strTenLoaiHopDong", strTenLoaiHopDong,
                                                        "strThoiGianNhac", strThoiGianNhac,
                                                        "strLoaiHopDongID",strLoaiHopDongID);
            command = new SqlCommand(UpdateCommand, conn);
            command.ExecuteNonQuery();
            conn.Close();
        }
        public void LoaiHopDongDelete(string strLoaiHopDongID)
        {
            conn.Open();
            string DeleteCommand = @"delete from LoaiHopDong where LoaiHopDongID = strLoaiHopDongID ";
            DeleteCommand = gb.Replace(DeleteCommand, "strLoaiHopDongID", strLoaiHopDongID);
            command = new SqlCommand(DeleteCommand, conn);
            command.ExecuteNonQuery();
            conn.Close();
        }
    }
}
